package com.fafa.swing.base;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: FocusListenerDemo
 * @date Date : 2019年11月24日 20:01
 */
public class FocusListenerDemo extends JFrame {

    JList list;
    JLabel label;
    JButton button;
    JTextField jTextField,jTextField2;

    public FocusListenerDemo(){
        setTitle("焦点事件监听器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(300,300,400,200);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        contentPane.setLayout(new BorderLayout(0,0));
        setContentPane(contentPane);
        label = new JLabel("        ");
        label.setFont(new Font("楷体",Font.BOLD,16));
        contentPane.add(label,BorderLayout.SOUTH);
        jTextField = new JTextField();
        jTextField2 = new JTextField();
        jTextField2.setColumns(10);
        jTextField.setFont(new Font("楷体",Font.BOLD,16));
        jTextField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //获取焦点时执行此方法
                label.setText("文本框获得焦点，正在输入内容");
            }

            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时执行的方法
                label.setText("文本框失去焦点，内容输入完毕");
            }
        });

        contentPane.add(jTextField2,BorderLayout.WEST);
        contentPane.add(jTextField);
    }

    public static void main(String[] args) {
        FocusListenerDemo focusListenerDemo = new FocusListenerDemo();
        focusListenerDemo.setVisible(true);
    }
}
